{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<br>
<button class="layui-btn  layui-btn-normal" id="add" type="button">
    <i class="layui-icon"></i> 新建用户
</button>

<script type="text/html" id="filemange_v1">
    <button type="button"  class="layui-btn layui-btn-xs" lay-event="save">更新</button>
    <button type="button"  class="layui-btn layui-btn-xs layui-btn-danger" lay-event="stop">停用</button>
</script>
<hr class="layui-bg-gray">
<table class="layui-hide" id="userTable" lay-filter="userTable"></table>
<script>
    layui.use(['table','code'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          var loading = layer.load();
          var  options = {
                elem:'#userTable',
                skin:"line",
                page:true,
                limit:10,
                limits:[10,20,30],
                height: 'full-200',
                toolbar: true,
                url:'/user/list',
                where:{
                },
                response:{
                    statusName:'errorCode',
                    statusCode:'',
                    dataName:'data'
                },
                cols:[[
                      {field: 'id', title: 'id',sort:true,width:80}
                      ,{field: 'userName', title: '用户名',sort:true}
                      ,{field: 'role', title: '角色',sort:true, templet: function (d) {
                            if(d.role == 0){
                                return '成员';
                             }else{
                                return '管理员';
                             }
                       }}
                      ,{field: 'project_name', title: '项目名称',sort:true}
                      ,{field: 'isShow', title: '状态',sort:true, templet: function (d) {
                         if(d.isShow == 0){
                            return '可用';
                         }else{
                            return '停用';
                         }
                      }}
                      ,{field: 'project_id', title: '项目Id',sort:true,hide:true}
                      ,{field: 'last_time', title: 'last_time',sort:true}
                      ,{fixed: 'right', title:'', toolbar: '#filemange_v1',width:150}
                ]],
                id:'userTableCache',
                done:function(res,cur,count){
                    layui.each(res.data,function(index,item){
                            if(item.isShow==1){
                                  $("tr[data-index='"+index+"']>td[data-field='isShow']").css('color','red');
                             }
                    });
                    layer.close(loading);

                }
              };
          var tableIns = table.render(options);
          //点击添加页面

          $('#add').on('click',function(){
                layui.data('test', {key: 'UserInfoCache',value: undefined});
                layer.open({
                            type:2,
                            content:'/user/manage',
                            area:['750px','410px'],
                            //offset: ['231px','307px'],
                            title:"新建用户",
                            closeBtn:1,
                            shade: 0,
                            shadeClose:true,
                            cancel: function(index, layero){
                                tableIns.reload(options);
                            }
                });
                return false;

          });
          table.on('row(projectCache)', function(obj){
                obj.tr.css("background","#eeeeee");
          });

          table.on('tool(userTable)', function(obj){

                if(obj.event === 'save'){
                    layui.data('test', {key: 'UserInfoCache',value: obj.data});
                    layer.open({
                            type:2,
                            content:'/user/manage',
                            area:['750px','410px'],
                            //offset: ['231px','307px'],
                            title:"新建用户",
                            closeBtn:1,
                            shade: 0,
                            shadeClose:true,
                            cancel: function(index, layero){
                                tableIns.reload(options);
                            }
                    });
                }else if(obj.event === 'stop'){
                    layer.confirm('真的停用么?', function(index){
                        $.ajax({
                            //发出请求
                            type:"POST",
                            url:"/user/delete",
                            contentType: 'application/json; charset=utf-8',
                            dataType:"json",
                            data:JSON.stringify({"id":obj.data.id}),
                            success: function(result){
                                if(result.id){
                                    layer.msg("已停用!");
                                    tableIns.reload(options);
                                }
                            }
                        });
                    });

                }
                return false;
          });
          form.render();
          layer.close(loading);
    });
</script>

{% endblock %}